package com.ds.smartstudio.controller;

import com.ds.smartstudio.common.Constants;
import com.ds.smartstudio.common.Result;
import com.ds.smartstudio.entity.Device;
import com.ds.smartstudio.entity.History;
import com.ds.smartstudio.service.DeviceService;
import com.ds.smartstudio.service.HistoryService;
import com.ds.smartstudio.service.impl.DeviceServiceImpl;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yangsf
 * @since 2022-07-02 10:42:24
 */
@RestController
@RequestMapping("/smartstudio/history")
public class HistoryController {
    @Autowired
    HistoryService historyService;

    @Autowired
    DeviceService deviceService;

    @RequestMapping(method = RequestMethod.HEAD)
    public History history() {return new History();}

    @GetMapping("/{mac}")
    public Result dayOfTemp(@PathVariable String mac) {
        Result result;
        try {
            List<History> histories = historyService.dayOfTemp(mac);
            if (histories == null) result = Result.error(Constants.CODE_500, "没有记录");
            else result = Result.succeed(histories);
        } catch (Exception e) {
            result = Result.error(Constants.CODE_500, "系统错误");
        }
        return result;
    }

    @GetMapping("/echars")
    public Result getTemp(@RequestParam Integer id) {
        Result result;
        try {
            List<History> histories = historyService.areaTemp(id);
            if (histories == null) result = Result.error(Constants.CODE_500, "没有记录");
            else result = Result.succeed(histories);
        } catch (Exception e) {
            result = Result.error(Constants.CODE_500, "系统错误");
        }
        return  result;
    }
    @GetMapping("/all/{mac}")
    public Result allOfTemp(@PathVariable String mac) {
        Result result;
        try {
            List<History> histories = historyService.allOfTemp(mac);
            if (histories == null) result = Result.error(Constants.CODE_500, "没有记录");
            else result = Result.succeed(histories);
        } catch (Exception e) {
            result = Result.error(Constants.CODE_500, "系统错误");
        }
        return result;
    }

    @GetMapping("/device")
    public List<List<String>> test01(@RequestParam Integer id)//根据场所ID传入历史表内的历史温度
    {
        List<Device> devices = deviceService.tempDevice(id);//获取到所传入场所的温度传感器的所有信息
        List<List<String>> lists = new ArrayList<>();
        List<String> tem = new ArrayList<>();
        for (Device i : devices) {
            List<History> histories = historyService.dayOfTemp(i.getDsDeviceMac());
            for (History j : histories) {
                tem.add(j.getDsHistoryValue().replace("℃", ""));

            }
            lists.add(tem);
        }
        return lists;
    }
    @ApiOperation(value = "分页查询")
    @GetMapping("/all/page")
    public List selectPage(@ApiParam(value = "当前页码", required = true)
                           @RequestParam int page,
                           @ApiParam(value = "页面大小", required = true)
                           @RequestParam int pageSize,
                           @ApiParam("设备名称")
                           @RequestParam(defaultValue = "")  String name) {
        return historyService.historyPage(page, pageSize, name);
    }

    @ApiOperation(value = "按区域筛选mac地址")
    @GetMapping("/all/area")
    public List selectMacInArea(
            @RequestParam String area
    ) {
        return historyService.MacInArea(area) ;
    }

    @ApiOperation(value = "按设备类型筛选mac地址")
    @GetMapping("/all/MacInType")
    public List selectMacInType(
            @RequestParam String Type
    ) {
        return historyService.MacInType(Type) ;
    }

    @ApiOperation(value = "页面条件查询历史记录")
    @GetMapping("/all/history")
    public List selectHistory(
            @RequestParam String Area,
            @RequestParam String Mac,
            @RequestParam String TimeStart,
            @RequestParam String TimeEnd,
            @RequestParam Integer page,
            @RequestParam Integer pageSize
    ) {
        return historyService.History(Area, Mac, TimeStart, TimeEnd,page,pageSize);
    }
}
